package cn.haocxy.jfm.gui.dialog;

import java.util.concurrent.atomic.AtomicBoolean;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;

public class LineInputDialog extends Dialog {

	private TextField textField;
	
	private Button ok;
	
	private Button cancel;
	
	private AtomicBoolean result = new AtomicBoolean(false);
	
	public LineInputDialog setText(String text) {
		textField.setText(text);
		textField.selectAll();
		return this;
	}
	
	public String getText() {
		return textField.getText();
	}
	
	@Override
	protected void setContentArea(AnchorPane pane) {
		textField = new TextField();
		pane.setPadding(new Insets(10.0));
		pane.getChildren().add(textField);
		
		AnchorPane.setTopAnchor(textField, 10.0);
		AnchorPane.setLeftAnchor(textField, 10.0);
		AnchorPane.setRightAnchor(textField, 10.0);
		
		setHeight(140);

		textField.addEventHandler(KeyEvent.KEY_RELEASED, e->{
			KeyCode code = e.getCode();
			if (code.equals(KeyCode.ENTER)) {
				result.set(true);
				hide();
				return;
			}
			if (code.equals(KeyCode.ESCAPE)) {
				hide();
				return;
			}
		});
	}
	
	@Override
	protected void setButtons() {
		
		ok = new DButton("确定");
		ok.setOnAction(e->{
			result.set(true);
			hide();
		});
		add(ok);
		
		cancel = new DButton("取消");
		cancel.setOnAction(e->{
			hide();
		});
		add(cancel);
	}
	
	@Override
	protected void onShowing() {
		textField.requestFocus();
	}

	@Override
	protected void onHiding() {
	}
	
}
